import validate from "./utils/validate";
import Log from "./constants/log";
import globalConfig from "./globalConfig";
import {
	navigateToEid,
	reportStat
} from "./utils/index";
const EIDAppID = "wx0e2cb0b052a91c92";

function initEid(e = "https://eid.faceid.qq.com", o = "release", n = "https://eid-enhance.faceid.qq.com") {
	uni.eidBaseUrl = e, uni.eidEnvVersion = o, uni.eidBackUpUrl = n;
	uni.onAppShow(e => {
		console.log("!!!!!!监听onshow事件", e, uni.eidTokenToCallback);
		var {
			scene: e,
			referrerInfo: o
		} = e, {
			appId: o,
			extraData: n
		} = o || {}, {
			token: i,
			verifyDone: t
		} = n || {};
		1038 === e && o === EIDAppID && n && (t && uni.handleEidVerifyDone ? uni.eidTokenToCallback && uni
			.eidTokenToCallback === i && (uni.eidTokenToCallback = "", uni.reportLogToEid({
				token: i,
				event: Log.navigateBackFromEid,
				errMsg: `从EID核身完成返回，token:${i},verifyDone:` + t
			}), console.log("!!!!!!执行回调"), uni.handleEidVerifyDone(n)) : uni.reportLogToEid({
				token: i,
				event: Log.navigateBackFromEidFail,
				errMsg: `核验未完成或者没有处理核验完成的函数，token:${i},verifyDone:` + t
			}))
	});
	e = uni.getSystemInfoSync();
	const d = e["version"];
	uni.reportLogToEid = function(e) {
		var {
			token: e = "",
			event: o = "",
			errCode: n = "",
			errMsg: i = "",
			data: t = {}
		} = e, r = new Date;
		const a = {
			Token: e,
			SourceType: Log.SourceType,
			SourceVersion: Log.version,
			EnvVersion: d,
			Timestamp: r.getTime(),
			Event: o,
			ErrorCode: "number" == typeof n ? n.toString() : n,
			ErrorMsg: i,
			Data: JSON.stringify(t)
		};
		console.log("开始上报日志：", a), uni.request({
			url: uni.eidBaseUrl + "/api/common/ReportEvent",
			method: "POST",
			data: a,
			success(e) {
				console.log("上报日志完成：", "payload:", a, "res:", e)
			}
		})
	}
}

function startEid(e) {
	const {
		data: o,
		verifyDoneCallback: n
	} = e;
	if (!o || !n) return uni.reportLogToEid({
		token: i,
		event: Log.startEidFail,
		errMsg: "传入的参数有误"
	}), void uni.showModal({
		title: "提示",
		content: "传入的参数有误",
		showCancel: !1
	});
	const {
		token: i,
		needJumpPage: t = !1,
		enableEmbedded: r = !1,
		allowFullScreen: a = !0
	} = o;
	validate.isValidateToken(i) ? (uni.enableEmbedded = r, uni.allowFullScreen = a, uni.VerifyEid_TOKEN = i,
reportStat({
		module: "EidStart",
		action: "enter"
	}), uni.handleEidVerifyDone = e => {
		const o = e["token"];
		reportStat({
			module: "EidEnd",
			action: "leave"
		}), t ? uni.navigateBack({
			success() {
				uni.reportLogToEid({
					token: o,
					event: Log.EidVerifyDone,
					errMsg: "验证完成，token：" + o
				}), n({
					token: o,
					verifyDone: !0
				})
			}
		}) : (uni.reportLogToEid({
			token: o,
			event: Log.EidVerifyDone,
			errMsg: "验证完成，token：" + o
		}), n({
			token: o,
			verifyDone: !0
		}))
	}, t ? uni.navigateTo({
		url: `${globalConfig.normalPath}/mp_ecard_sdk/index/index?token=${i}&needJumpPage=` + t
	}) : navigateToEid(i)) : (uni.reportLogToEid({
		token: i,
		event: Log.startEidFail,
		errMsg: "传入的token有误，token:" + i
	}), uni.showModal({
		title: "提示",
		content: "传入的token有误",
		showCancel: !1
	}))
}
export {
	initEid,
	startEid
};